<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Pictures{

        public function __construct(){

            $this->CI = &get_instance();
            $this->CI->load->model('pictures_model');
        }

        /**
        * 
        * Show error message
        * @param $str error string
        */        
        private function throwError($str){
            echo $str;
            exit();
        }

        public function create_thumbnails($folder, $pictures,$thumbnails_sizes)
        {
            // load generator library
            $this->CI->load->library('generator');
            $this->CI->generator->set_jpeg_quality(100);

            // get reference to images on server
            $imagespath = './'.config_item('gallery_folder').'/'.$folder.'/';
            $this->CI->generator->setImagesFolder($imagespath);

            // for each picture, cretae thumbnails and put in prepared folder
            foreach ($pictures as $picture) :

                foreach ($thumbnails_sizes as $size) :
                    //sleep(1);
                    $filesave = $imagespath.'/thumb_'.$size['w'].'x'.$size['h'].'/'.$picture;
                    $this->CI->generator->thumbnail($picture,$size['w'],$size['h'],$filesave);

                    endforeach;

                endforeach;

            return TRUE;
        } 

        public function create_records($pictures,$titles,$gallery_id)
        {
            $count = count($pictures); 
            for($i=0;$i<$count;$i++):

                $data = array(
                    'filename'=> $pictures[$i],
                    'name'=> $titles[$i],
                    'date_time'=> time(),
                    'gallery_ID'=> $gallery_id
                );
                $insert_id = $this->CI->pictures_model->insert($data);
                if(!$insert_id) $this->throwError('This should not happend :)');
                endfor;

            return TRUE;
        }

        function find_picture_column($picture_id,$column)
        {
            // get gallery folder name
            $picture = $this->CI->pictures_model->find_picture_column($picture_id);
            if(!isset($picture['filename']))
            {
                $this->throwError('No picture associated with this ID :)');
                return;
            }
            return $picture[$column];
        }

        public function delete($picture_id)
        {
            $this->CI->pictures_model->delete($picture_id);
        }

        public function delete_on_server($folder,$filename,$thumbnails_sizes)
        {
            $path = './'.config_item('gallery_folder').'/'.$folder.'/';

            // delete original picture from root gallery folder
            if (file_exists($path.$filename)) 
                unlink($path.$filename);

            // delete thumbnails pictures
            foreach ($thumbnails_sizes as $size) :

                $thumbnail = $path.'/thumb_'.$size['w'].'x'.$size['h'].'/'.$filename;
                if (file_exists($thumbnail)) 
                    unlink($thumbnail);

                endforeach;

        }

        public function order($pictures,$order)
        {
            $count = count($pictures);
            for($i=0;$i<$count;$i++){
                $this->CI->pictures_model->update($pictures[$i]['ID'],array('order'=>$order[$i]));
            }
        }

    }

?>
